package cn.edu.pku.cs08.liudun.picventure;

import java.io.File;

import cn.edu.pku.cs08.liudun.picventure.activity.WorkbenchActivity;
import cn.edu.pku.cs08.liudun.picventure.tools.SwitchActivityTools;
import android.app.Activity;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;

public class PicventureActivity extends Activity{
	private static final String TAG = "PicventureActivity";
	 
	ImageView imageView = null;
	AppConfig appConfig = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.picventure);
	        Log.e(TAG, "start picventure");
	        
	        createAppDir();
	        
	        imageView = (ImageView) this.findViewById(R.id.imageViewWelcome);
	        
	        SwitchActivityTools.SetButton(R.id.imageViewWelcome, 
	        		this, WorkbenchActivity.class, true);
	        appConfig = (AppConfig) this.getApplicationContext();
	        initScreenRectF();
	}
	
	private void initScreenRectF() {
		RectF screenPortraitRectF = new RectF(0, 0, 
				this.getResources().getDisplayMetrics().widthPixels-1, 
				this.getResources().getDisplayMetrics().heightPixels-1);
		appConfig.setScrenPortraitRectF(screenPortraitRectF);
		RectF screenLandscapeRectF = new RectF(0, 0, 
				this.getResources().getDisplayMetrics().heightPixels-1, 
				this.getResources().getDisplayMetrics().widthPixels-1);
		appConfig.setScrenLandscapeRectF(screenLandscapeRectF);
		Log.e(TAG, "initScreenPortraitRectF: " + this.getResources().getDisplayMetrics().widthPixels
				+ " " + this.getResources().getDisplayMetrics().heightPixels);
		Log.e(TAG, "initScreenLandscapeRectF: " + this.getResources().getDisplayMetrics().heightPixels
				+ " " + this.getResources().getDisplayMetrics().widthPixels);
	}
	
	private boolean createAppDir() {
		String status = Environment.getExternalStorageState();
		String sdcardPath = null;
		if(status.equals(Environment.MEDIA_MOUNTED)) {
			sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
			Log.e(TAG, sdcardPath + " is available");
		}
		else {
			Log.e(TAG, "externalStorage is not available");
			return false;
		}
		
		File appDir = new File(sdcardPath + "/Picventure");
		
		if(appDir.exists()) {
			Log.e(TAG, appDir.getAbsolutePath() + " already exists");
			if(appDir.isDirectory()) {
				Log.e(TAG, appDir.getAbsolutePath() + " is a directory");
				File imageDir = new File(appDir.getAbsolutePath() + "/image");
				if(imageDir.exists()) {
					Log.e(TAG, imageDir.getAbsolutePath() + " already exists");
					if(imageDir.isDirectory()) {
						Log.e(TAG, imageDir.getAbsolutePath() + " is a directory");
					}
					else if(imageDir.isFile()) {
						Log.e(TAG, imageDir.getAbsolutePath() + " is a file");
						//TODO
						return false;
					}
				}
				else {
					if(imageDir.mkdir()) {
						Log.e(TAG, imageDir.getAbsolutePath() + " created");
					}
					else {
						Log.e(TAG, imageDir.getAbsolutePath() + " can not be created");
						return false;
					}
				}
			}
			else if(appDir.isFile()) {
				Log.e(TAG, appDir.getAbsolutePath() + " is a file");
				//TODO
				return false;
			}
		}
		else {
			if(appDir.mkdir()) {
				Log.e(TAG, appDir.getAbsolutePath() + " created");
				File imageDir = new File(appDir.getAbsolutePath() + "/image");
				if(imageDir.mkdir()) {
					Log.e(TAG, imageDir.getAbsolutePath() + " created");
				}
				else {
					Log.e(TAG, imageDir.getAbsolutePath() + " can not be created");
					return false;
				}
			}
			else {
				Log.e(TAG, appDir.getAbsolutePath() + " can not be created");
				return false;
			}
		}
		return true;
	}

	
}
